package com.example.iconmaker;

import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.Bitmap;
import android.opengl.GLUtils;

public class Wheel {

	public static final float ANGLESPACE = 25;
	public static final float DECELERATE = 0.95f;
	public static final float MIN_SPEED = 0.4f; // 最小速度
	public static final float MORE_ANGEL = 10f; // 图标转出屏幕这个角度仍然会被显示

	public static final int HALF_AHGLE = 180;
	public static final int RIGHT_SIDE = -1;
	public static final int LEFT_SIDE = 1;
	private List<Icon> icons = new ArrayList<Icon>();
	public float mAngle = 0;// 总体旋转角度
	private int mCount = 0;

	public float mSpeed = 0;
	/**
	 * 类型 ，值为RIGHT_SIDE 或 LEFT_SIDE
	 */
	public int type;

	/**
	 * 半径
	 */
	public float radiu;

	/**
	 * 角度间隔
	 */
	public float centreX;
	public float contreY;

	public Wheel(int type, float radiu) {
		super();
		this.type = type;
		this.radiu = radiu;
	}

	public void draw(GL10 gl) {

		// 先平移到转轮圆心并旋转总体角度
		gl.glTranslatef(centreX, contreY, 0.0f);
		gl.glRotatef(mAngle, 0, 0, 1.0f * type);

		int i = 0;
		float cAngle = 0;
		// 画出每个图标
		do {
			cAngle = (ANGLESPACE * i + mAngle);
			// 只有看得到的才会画出来
			if (cAngle >= -HALF_AHGLE / 2 - MORE_ANGEL && cAngle <= HALF_AHGLE / 2 + MORE_ANGEL) {
				icons.get(i).draw(gl);
			}
			gl.glRotatef(ANGLESPACE, 0, 0, 1.0f * type);
			i++;
		} while (i < mCount);
		setAngle(mAngle + mSpeed);
		if (mSpeed != 0) {
			decelerate();
		}

		// mAngle %= HALF_AHGLE * 2;
	}

	public void add(Icon icon) {
		icons.add(icon);
	}

	public void add(float size, int texture) {
		float s = size / 2;
		icons.add(new Icon(-s + radiu * type, s, s + radiu * type, -s, texture));
		mCount++;
	}

	public void initTexture(List<Bitmap> bitmaps, GL10 gl) {
		int size = bitmaps.size();

		IntBuffer textureBuffer = IntBuffer.allocate(size);
		gl.glGenTextures(size, textureBuffer);
		int[] textures = textureBuffer.array();
		for (int i = 0; i < size; i++) {
			Bitmap b = bitmaps.get(i);
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]);
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, b, 0);
			gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
			gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		}

		for (int i = 0; i < size; i++) {
			add(Icon.DEFUALT_SIZE, textures[i]);
		}
	}

	public void setAngle(float angle) {
		if (angle > 0) {
			this.mAngle = 0;
		} else if (angle < -(mCount - 1) * ANGLESPACE) {
			this.mAngle = -(mCount - 1) * ANGLESPACE;
		} else {
			this.mAngle = angle;
		}
	}

	/**
	 * 功能简述:减速并对齐 功能详细描述: 注意:
	 */
	private void decelerate() {
		mSpeed *= DECELERATE;
		// 如果当前速度小于最小速度并且还未对齐，就要进行对齐
		if (Math.abs(mSpeed) < MIN_SPEED) {
			align();
		}
	}

	/**
	 * 功能简述:对齐 功能详细描述: 注意:
	 */
	public void align() {
		float space = mAngle % ANGLESPACE;
		if (Math.abs(space) < MIN_SPEED) {
			mSpeed = 0;
		} else {
			// mSpeed = mSpeed > 0 ? MIN_SPEED : -MIN_SPEED;
			mSpeed = Math.abs(space) > ANGLESPACE / 2 ? -MIN_SPEED : MIN_SPEED;
		}
	}
}
